Skip to content

Playwright fix test failures and improve the pytest_runtest_makereport hook #6599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,11 @@ def test_loginless_mozilla_account_aaq(page: Page):
"blocked after 3 submissions"):
i = 1
while i <= 4:
sumo_pages.top_navbar.click_on_signin_signup_button()
# In case a 502 error occurs we might end up in the auth page after the automatic
# refresh/retry so we need to skip the signin_signup button click since the
# element is not available.
if sumo_pages.top_navbar.is_sign_in_up_button_displayed():
sumo_pages.top_navbar.click_on_signin_signup_button()
sumo_pages.auth_page.click_on_cant_sign_in_to_my_mozilla_account_link()
sumo_pages.aaq_flow.submit_an_aaq_question(
subject=utilities.aaq_question_test_data['premium_aaq_question']['subject'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1332,8 +1332,9 @@ def test_solves_this_problem(page: Page):
sumo_pages.aaq_flow.deleting_question_flow()


# Need to add test for preview as well.
# T5696791, T5696772, T5696774, T5696776, T5696792
# Skipped until we decide if we should revert https://github.com/mozilla/sumo/issues/2245 or not
@pytest.mark.skip
@pytest.mark.postedQuestions
@pytest.mark.parametrize("quote_on", ['reply', 'question'])
def test_quote_reply_functionality(page: Page, quote_on):
Expand Down
45 changes: 19 additions & 26 deletions playwright_tests/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,29 @@ def handle_502_error(response):
return page


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call) -> None:
"""
This pytest hook is triggered after each test execution.
If a test failure occurred we are saving & attaching the test execution screencast to the
allure report for better debugging.
If a test failure occurred (including pytest assertion failures) we are saving & attaching the
test execution screencast to the allure report for better debugging.
"""
if call.when == "call":
# Check if the test has raised an exception (test failed or encountered an error during
# execution). Also checks if the test function has the page instance in its arguments
# ensuring that video recording is applied only when the test involves playwright
# automation.
if call.excinfo is not None and "page" in item.funcargs:
# Retrieve the page object from the test function arguments.
page: Page = item.funcargs["page"]
# Provide the path to the recorded video (the video recording starts when the browser
# context is created).
video_path = page.video.path()
# Ensure that the browser context is closed after test. Closing the context also
# ensures that the video is properly saved.
page.context.close()
# Attaching the video to the Allure report:
# 1. Opening the video file in binary mode and reading its content.
# 2. Assigning a name to the video attachment based on the slugyfied version of the
# test node id.
# 3. Saving the video as a .webm extension.
allure.attach(
open(video_path, 'rb').read(),
name=f"{slugify(item.nodeid)}.webm",
attachment_type=allure.attachment_type.WEBM
)

outcome = yield # Capture the result of the test execution.
report = outcome.get_result() # Retrieve the test execution report.

# Ensure the test has failed and involves Playwright automation.
if report.failed and "page" in item.funcargs:
page: Page = item.funcargs["page"] # Retrieve the page object from the test function args.
video_path = page.video.path() # Retrieve the path to the recorded video.
page.context.close() # Close the browser context to ensure the video is properly saved.

# Attaching the video to the Allure report:
allure.attach(
open(video_path, 'rb').read(),
name=f"{slugify(item.nodeid)}.webm",
attachment_type=allure.attachment_type.WEBM
)


@pytest.fixture()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ def test_explore_by_topic_product_filter(page: Page):
if product.strip() == "All Products":
continue
else:
sumo_pages.explore_by_topic_page.select_a_filter_by_product_option(
product.strip())
time.sleep(2)
with page.expect_navigation(timeout=3000) as navigation_info:
sumo_pages.explore_by_topic_page.select_a_filter_by_product_option(
product.strip())
response = navigation_info.value
if response is None:
print("Navigation did not occur. Refreshing the page.")
utilities.refresh_page()
if not sumo_pages.explore_by_topic_page.get_metadata_of_all_listed_articles():
pytest.fail(f"There is no sublist for {product}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ def test_group_messages_cannot_be_sent_by_non_staff_users(page: Page):
)

with allure.step("Verifying that no users are returned"):
expect(sumo_pages.new_message_page.get_no_user_to_locator()).to_be_visible(timeout=10000)
expect(sumo_pages.new_message_page.get_no_user_to_locator()).to_be_visible(timeout=15000)

with allure.step("Navigating to the groups page"):
utilities.navigate_to_link(utilities.general_test_data['groups'])
Expand Down Expand Up @@ -1053,7 +1053,7 @@ def test_unable_to_send_group_messages_to_profiless_groups(page: Page):
sumo_pages.new_message_page.type_into_new_message_to_input_field("kb-contributors")

with allure.step("Verifying that no users are returned"):
expect(sumo_pages.new_message_page.get_no_user_to_locator()).to_be_visible(timeout=10000)
expect(sumo_pages.new_message_page.get_no_user_to_locator()).to_be_visible(timeout=15000)


# C2083482
Expand Down